# -*- coding: utf-8 -*-
from rest_framework import serializers
from core.serializers import CustomModelSerializer
from core.relations import BaseNameWritableRelatedField
from apps.tms.models import TestCase
from apps.sys.models import DictData


class TestCaseSerializer(CustomModelSerializer):
    """
    用例详情-序列化器
    """

    product = BaseNameWritableRelatedField()
    module = BaseNameWritableRelatedField()
    reqs = BaseNameWritableRelatedField(many=True)
    bug_set = BaseNameWritableRelatedField(
        many=True, required=False, allow_empty=True, allow_null=True
    )

    class Meta:
        model = TestCase
        fields = "__all__"
        read_only_fields = ["id"]


class TestCaseTreeSelectSerializer(CustomModelSerializer):
    """
    用例选择-序列化器
    """

    name = serializers.SerializerMethodField(read_only=True)
    type = serializers.SerializerMethodField(read_only=True)

    def get_name(self, instance):
        level_lable = DictData.objects.get(
            type="tms_tc_level", value=instance.level
        ).label
        return f"【{level_lable}】{instance.name}"

    def get_type(self, instance):
        return instance._meta.object_name

    class Meta:
        model = TestCase
        fields = ("id", "name", "number", "type")
        read_only_fields = ["id"]
